﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HAMath.InverseDistance
{

    /// <summary>
    /// 反距离平方法插值
    /// </summary>
   public  class InverseDistanceWeight2D
    {


        /// <summary>
        /// 反距离平方法对点（x，y）插值
        /// </summary>
        /// <param name="point3d">观测点的信息</param>
        /// <param name="_centerPoint">采样点（参考点）的信息（</param>
        /// <param name="_planeCenterPointElevation">采样点（参考点）的Scalar(高程，浓度等等)信息  注意 ：顺序要和第二个参数的顺序一致</param>
        /// <returns></returns>
        public static double GetPointElevation(Point2D  point2d, List<Point2D> _centerPoint, List<double> _planeCenterPointScalar)
        {
            double n = 2.0;
            if (_centerPoint.Count == 0)
            {
                return 0.0;
            }
            double D = 0.0;//距离的乘方倒数之和
            double DB = 0.0;
            for (int i = 0; i < _centerPoint.Count; i++)
            {
                double Distance = GetDistance(point2d, _centerPoint[i]);
                if (Distance <= 0.0)
                {
                    D = 0.0;
                    return _planeCenterPointScalar[i];
                    //break;
                }
                else
                {
                    //double Di = 1 / Distance ^ n;
                    double Di = 1 / (Distance * Distance);///
                    D += Di;

                }
            }

            for (int i = 0; i < _centerPoint.Count; i++)
            {
                double Distance = GetDistance(point2d, _centerPoint[i]);
                if (Distance <= 0.0)
                {
                    D = 0.0;
                    return _planeCenterPointScalar[i];
                }
                else
                {
                    //double Di = 1 / Distance ^ n;
                    double Di = 1 / (Distance * Distance);/////////////////////////////
                    DB += _planeCenterPointScalar[i] * Di / D;
                }
            }

            return DB;

        }
        /// <summary>
        /// 计算观测点和采样点（参考点）的平面距离 
        /// </summary>
        /// <param name="loc1">观测点</param>
        /// <param name="loc2">采样点（参考点）</param>
        /// <returns></returns>
        private static double GetDistance(Point2D loc1, Point2D loc2)
        {
            double _distance, d1, d2;
            d1 = loc1.X - loc2.X;
            d2 = loc1.Y - loc2.Y;
          

            _distance = Math.Sqrt(d1 * d1 + d2 * d2);
          

            return _distance;
        }








    }
}
